/**
 * Created by syf on 2016/1/8.
 */
grApp.controller('menuManageCtr', ['$scope', '$http', 'DatabaseFactory2', function ($scope, $http, DatabaseFactory2) {
    //$scope.save_menu = function () {
    //    alert(1111);
    //}
    $scope.save_menu = function () {
        DatabaseFactory2.save_menu({
            title: $scope.menuTitle,
            sort: $scope.menuSort,
            url: $scope.menuLink,
            pid: $scope.menuLevel,
            group: $scope.menuGroup,
            tip: $scope.menuDescribe
        }).then(function (data) {
            if (data.id > 0) {
                showTips("success", "增加成功");
                window.location.href = '/basedata/menuManage';
            } else {
                showTips("error", "增加失败");
            }
        });
    };
}])
grApp.controller('menuListCtr', ['$scope', '$http', 'DatabaseFactory2', function ($scope, $http, DatabaseFactory2) {
    var GetAllMenu = function () {
        var pageInfo = {
            pageIndex: $scope.paginationConf.currentPage,
            pageSize: $scope.paginationConf.itemsPerPage
        };
        $scope.menuListM = [];
        DatabaseFactory2.menuList(pageInfo).then(function (data) {
            $scope.menuListM = data;
        })
        DatabaseFactory2.menuAllCount().then(function (data) {
            $scope.paginationConf.totalItems=data[0].count;
        })
    }

    ////配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 0,
        itemsPerPage: 8,
        pagesLength: 9,
        perPageOptions: [10, 20, 30, 40, 50],
        rememberPerPage: 'perPageItems'
    };
    /***************************************************************
     当页码和页面记录数发生变化时监控后台查询
     如果把currentPage和itemsPerPage分开监控的话则会触发两次后台事件。
     ***************************************************************/
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetAllMenu);


    $scope.delete=function(index){
        DatabaseFactory2.deleteMenu($scope.menuListM[index].id).then(function(data){
            if(data.msg=="success"){
                showTips('success','删除成功');
                $scope.menuListM.splice($scope.menuListM.indexOf($scope.menuListM[index]),1);
            }
        })
    }
}])

grApp.factory('DatabaseFactory2', ['$http', '$q', function ($http, $q) {
    var service = {};
    service.save_menu = function (args) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/basedata/add_Menu',
            params: args
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    service.menuList = function (pageInfo) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'get',
            url: '/basedata/menuList',
            params:pageInfo
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.menuAllCount = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'get',
            url: '/basedata/menuAllCount'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.deleteMenu=function(id){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        var parms={"menuid":id};
        $http({
            method: 'get',
            url: '/basedata/deleteMenu',
            params:parms
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    }
    return service;
}])
